package com.qf.shiro2202day02.shiroconfig;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        chainDefinition.addPathDefinition("/register.html", "anon");
        chainDefinition.addPathDefinition("/user/login", "anon");
        chainDefinition.addPathDefinition("/user/register", "anon");
        chainDefinition.addPathDefinition("/error", "anon");
        //放行报错
        chainDefinition.addPathDefinition("/home.html","user");
        chainDefinition.addPathDefinition("/**", "authc");
        return chainDefinition;
    }

    @Bean
    public Realm customRealm() {
        CustomRealm customRealm = new CustomRealm();
        //密码匹配器对象
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("md5");
        credentialsMatcher.setHashIterations(1024);
        //设置加密方式
        customRealm.setCredentialsMatcher(credentialsMatcher);
        // 开启缓存
        customRealm.setCachingEnabled(true);
        // 开启身份认证缓存
        customRealm.setAuthenticationCachingEnabled(true);
        // 开启授权数据缓存
        customRealm.setAuthorizationCachingEnabled(true);
        return customRealm;
    }
}
